package com.zens.cms.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.zens.cms.entity.SysConfig;
import com.zens.cms.entity.SysConfigExample;
import com.zens.cms.entity.SysConfigExample.Criteria;
import com.zens.cms.mapper.SysConfigMapper;
import com.zens.cms.services.ISysConfig;

@Service
public class SysConfigService implements ISysConfig {

	@Resource
	SysConfigMapper mapper;
	@Override
	public int insert(SysConfig config) {
		return mapper.insertSelective(config);
	}

	@Override
	public int update(SysConfig config) {
		return mapper.updateByPrimaryKeySelective(config);
	}

	@Override
	public List<SysConfig> query(SysConfig config) {
		SysConfigExample example = new SysConfigExample();
		if(config != null){
			Criteria criteria = example.createCriteria();
			if(StringUtils.hasText(config.getFkey()))
				criteria.andFkeyEqualTo(config.getFkey());
			if(config.getId() != null)
				criteria.andIdEqualTo(config.getId());
		}
		return mapper.selectByExample(example);
	}

	@Override
	public SysConfig queryByIdOrKey(SysConfig config) {
		SysConfigExample example = new SysConfigExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.hasText(config.getFkey()))
			criteria.andFkeyEqualTo(config.getFkey());
		if(config.getId() != null)
			criteria.andIdEqualTo(config.getId());
		List<SysConfig> configs = mapper.selectByExample(example);
		
		return configs.isEmpty() ? null : configs.get(0);
	}

	@Override
	public int delete(List<Long> id) {
		SysConfigExample example = new SysConfigExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(id);
		return mapper.deleteByExample(example);
	}

}
